import { _decorator, Component, Label, Node, Sprite, tween, Vec3, VerticalTextAlignment } from 'cc';
import { DIRECTION, GlobalData } from '../../../Data/GlobalData';
import { BrickInfo } from '../../../Data/GlobaInterface';
const { ccclass, property } = _decorator;

@ccclass('SingleBrick')
/**
 * 单个砖块
 */
export class SingleBrick extends Component {

    //砖头纹理
    @property(Sprite)
    private Brick: Sprite = null;

    //被选择样式
    @property(Node)
    private ChooseBg: Node = null;

    //砖块信息
    public BrickInfo: BrickInfo = {
        HorId: 0,
        VerId: 0,
        ClearIndex: -1,
    };
    //暂存砖块信息
    private TempBrickInfo: BrickInfo = {
        HorId: 0,
        VerId: 0,
        ClearIndex: -1,
    };
    //暂存位置
    private TempPos: Vec3 = new Vec3(0, 0, 0);

    //测试文字
    @property(Label)
    private TestLabel: Label = null;

    //是否效果砖块
    public IsEffect: boolean = false;

    /**
     * 更新我的横纵向id
     */
    public UpdateMyId(_hor: number, _ver: number) {
        this.BrickInfo.HorId = _hor;
        this.BrickInfo.VerId = _ver;

        this.TestLabel.string = "横" + this.BrickInfo.HorId + " 纵" + this.BrickInfo.VerId;
    }

    /**
     * 更新我得消除索引
     */
    public UpdateClearIndex(_index: number) {
        this.BrickInfo.ClearIndex = _index;
        this.ChooseBg.active = false;
        this.Brick.spriteFrame = app.resManager.SpriteFrames["game_cell" + (this.BrickInfo.ClearIndex + 1)]
    }

    /**
     * 我被选中了
     */
    public IamChoose() {
        this.ChooseBg.active = true;
    }

    /**
     * 我取消选中
     */
    public IamUnChoose() {
        this.ChooseBg.active = false;
    }

    /**
     * 我是效果砖块
     */
    public ImaEffect() {
        this.IsEffect = true;
    }

    /**
     * 交换位置
     * @param 对方信息
     */
    public ChangePos(_brickinfo: BrickInfo) {
        console.log("交换的信息", _brickinfo)
        console.log("我的信息", this.BrickInfo)
        let horid: number = _brickinfo.HorId;
        let verid: number = _brickinfo.VerId;

        if (this.BrickInfo.HorId < horid) {
            this.PlayChangePosAnim(DIRECTION.LEFT, horid, verid, _brickinfo.ClearIndex);
        }

        if (this.BrickInfo.HorId > horid) {
            this.PlayChangePosAnim(DIRECTION.RIGHT, horid, verid, _brickinfo.ClearIndex);
        }

        if (this.BrickInfo.VerId < verid) {
            this.PlayChangePosAnim(DIRECTION.UP, horid, verid, _brickinfo.ClearIndex);
        }

        if (this.BrickInfo.VerId > verid) {
            this.PlayChangePosAnim(DIRECTION.DOWN, horid, verid, _brickinfo.ClearIndex);
        }

    }

    /**
     * 更新暂存砖块信息
     */
    public UpdateTempBrickInfo() {
        this.TempBrickInfo.ClearIndex = this.BrickInfo.ClearIndex;
    }

    /**
     * 播放换位置动画
     * @param 方向
     */
    public PlayChangePosAnim(_dir: string, _horid: number, _verid: number, _clearindex: number) {
        let _pos: Vec3 = new Vec3(0, 0, 0);
        switch (_dir) {
            case DIRECTION.UP:
                _pos = new Vec3(0, GlobalData.BrickSpace, 0);
                break;
            case DIRECTION.DOWN:
                _pos = new Vec3(0, -GlobalData.BrickSpace, 0);
                break;
            case DIRECTION.LEFT:
                _pos = new Vec3(GlobalData.BrickSpace, 0, 0);
                break;
            case DIRECTION.RIGHT:
                _pos = new Vec3(-GlobalData.BrickSpace, 0, 0);
                break;
        }

        this.TempPos = _pos;

        tween(this.Brick.node).to(GlobalData.BrickMoveTime, { position: _pos }).call(() => {
            this.UpdateTempBrickInfo();
            this.Brick.node.setPosition(new Vec3(0, 0, 0))
            this.UpdateClearIndex(_clearindex);
        }).start();
    }

    /**
     * 恢复原位置
     */
    public RevertPos() {
        this.Brick.node.setPosition(this.TempPos);
        this.UpdateClearIndex(this.TempBrickInfo.ClearIndex);
        tween(this.Brick.node).to(GlobalData.BrickMoveTime, { position: new Vec3(0, 0, 0) }).start();
    }

    /**
     * 我直接移动到最高的位置
     */
    public PlayDownAni(_num: number) {
        this.Brick.node.setPosition(new Vec3(0, _num * GlobalData.BrickSpace, 0));//指定高度的位置
        tween(this.Brick.node).to(GlobalData.BrickDownSpeed, { position: new Vec3(0, 0, 0) }).start();
    }

}


